package techanic;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.das.mechanic_base.init.AutoX3SDK;
import com.das.technician.R;
import com.google.gson.Gson;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class MainActivity1 extends AppCompatActivity implements View.OnClickListener {

    TextView tv_click;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main1);
        tv_click = findViewById(R.id.tv_click);
        tv_click.setOnClickListener(this);

//        new Thread(){
//            @Override
//            public void run() {
//                super.run();
//                requestAccessKey();
//            }
//        }.start();

    }

    private void requestAccessKey() {
        OkHttpClient client = new OkHttpClient();

        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"),
                "{\"appKey\":\'" + Autox3Constant.appKey + "\'" + ", \"appSecret\":\'" + Autox3Constant.appSecret + "\'" + '}');
        Request request = new Request.Builder()
                .url(Autox3Constant.OpenBaseUrl + "/openapi/api-v1/auth")
                .post(body)
                .build();
        Response execute = null;
        try {
            execute = client.newCall(request).execute();
            String string = execute.body().string();
            Gson gson = new Gson();
            Log.e("SSSS","获取assesskey:" + string);
            OpenApiBean openApiBean = gson.fromJson(string, OpenApiBean.class);
            DataBean dataBean = gson.fromJson(openApiBean.data.toString(),DataBean.class);
            String accessKey = dataBean.accessToken;

            /**
             * accessKey 第三方平台获取到的AutoX3的访问凭证
             * OpenBaseUrl AutoX3提供给第三方平台的开放请求域名
             */
            AutoX3SDK.login(accessKey,Autox3Constant.OpenBaseUrl);

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public void onClick(View v) {
        AutoX3SDK.jumpVehicleReceiptList(null);
    }
}